<!DOCTYPE html>
            
<HTML>
<HEAD>
<meta name="booktitle" content="Developing Applications With Objective Caml" >
 <meta charset="ISO-8859-1"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<META name="GENERATOR" content="hevea 1.05-7 of 2000-02-24">
<META NAME="Author" CONTENT="Christian.Queinnec@lip6.fr">
<LINK rel=stylesheet type="text/css" href="videoc-ocda.css">
<script language="JavaScript" src="videoc.js"><!--
//--></script>
<TITLE>
 Steps of Compilation
</TITLE>
</HEAD>
<BODY class="regularBody">
<A HREF="book-ora064.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora066.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2> Steps of Compilation</H2>
An executable file is obtained by translating and linking
as described in figure <A HREF="book-ora065.html#fig-link">7.1</A>.<BR>
<BR>
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Source program</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>preprocessing</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Source program</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>compiling</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Assembly program</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>assembling</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Machine instructions</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>linking</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Executable code</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR></TABLE>
<BR>
<DIV ALIGN=center>Figure 7.1: Steps in the production of an executable.</DIV><BR>

<A NAME="fig-link"></A>
</DIV>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>To start off, preprocessing replaces certain pieces of text by other text according to
a system of
macros. Next, compilation translates the source program into assembly
instructions, which are
then converted to machine instructions. Finally, the linking process establishes a 
connection to the operating system for primitives. This includes adding the runtime
library, which mainly consists of memory management routines. <P><BR>
<BR>
<A NAME="toc90"></A>
<H3> The Objective CAML Compilers</H3>
The code generation phases of the Objective CAML compiler
are detailed in figure <A HREF="book-ora065.html#fig-compil">7.2</A>. The internal representation of the code generated 
by the compiler is called an intermediate language
(IL).<BR>
<BR>
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<TABLE CELLSPACING=2 CELLPADDING=0>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Sequence of characters</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>lexical analysis</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Sequence of lexical elements</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>parsing</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Syntax tree</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>semantic analysis</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Annotated syntax tree</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>generation of intermediate code</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Sequence of IL</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>optimization of intermediate code</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Sequence of IL</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>generation of pseudo code</TD>
<TD  ALIGN=center NOWRAP><FONT FACE=symbol>�</FONT></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR>
<TR><TD  ALIGN=left NOWRAP>&nbsp;</TD>
<TD  ALIGN=center NOWRAP><span style='color: blue'>Assembly program</span></TD>
<TD  ALIGN=left NOWRAP>&nbsp;</TD>
</TR></TABLE>
<BR>
<DIV ALIGN=center>Figure 7.2: Compilation stages.</DIV><BR>

<A NAME="fig-compil"></A>
</DIV>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>The lexical analysis stage transforms a sequence of characters to a sequence of
lexical
elements. These lexical entities correspond principally to integers, floating point
numbers,
characters, strings of characters and identifiers. The message <TT>Illegal
character</TT> might
be generated by this analysis.<BR>
<BR>
The parsing stage constructs a syntax tree and verifies that the
sequence of lexical
elements is correct with respect to the grammar of the language. The message
<CODE>Syntax error</CODE>
indicates that the phrase analyzed does not follow the grammar of the
language.<BR>
<BR>
The semantic analysis stage traverses the syntax tree, checking another
aspect of program correctness. The analysis consists principally of type
inference, which if successful, produces the <EM>most general type</EM>
of an expression or declaration. Type error messages may occur during
this phase. This stage also detects whether any members of a sequence
are not of type <I>unit</I>. Other warnings may result, including
pattern matching analysis (e.g <TT>pattern matching is not exhaustive</TT>,
<TT>part of pattern matching will not be used</TT>).<BR>
<BR>
Generation and the optimization of intermediate code does not produce
errors or warning messages.<BR>
<BR>
The final step in the compilation process is the generation of a program
binary. Details differ from compiler to compiler.<BR>
<BR>
<A NAME="toc91"></A>
<H3> Description of the Bytecode Compiler</H3>
<A NAME="@concepts121"></A>
<A NAME="@concepts122"></A>
<A NAME="@concepts123"></A>
<A NAME="@concepts124"></A>The Objective CAML virtual machine is called <EM>Zinc</EM> (<I>``Zinc Is Not Caml''</I>). 
Originally created by Xavier Leroy, <EM>Zinc</EM> is described in ([<A HREF="book-ora214.html#Leroy-ZINC"><CITE>Ler90</CITE></A>]). <EM>Zinc</EM>'s name was chosen to
indicate its
difference from the first implementation of Caml on the virtual machine CAM
(Categorical Abstract Machine, see [<A HREF="book-ora214.html#cousineau-et-al:1987"><CITE>CCM87</CITE></A>]).<BR>
<BR>
Figure <A HREF="book-ora065.html#fig-zinc">7.3</A> depicts the
bytecode compiler. The first part of this figure shows the
Zinc machine interpreter, linked to the runtime library. The second part corresponds to the
Objective CAML bytecode compiler
which produces instructions for the Zinc machine. The third part contains
the set of
libraries that come with the compiler. They will be described in Chapter <A HREF="index.html#chap-Bibliotheques">8</A>.
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<IMG SRC="book-ora026.gif">
<BR>
<BR>
<DIV ALIGN=center>Figure 7.3: Virtual machine.</DIV><BR>

<A NAME="fig-zinc"></A>
</DIV>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>
Standard compiler graphical notation is used for describing the components in figure <A HREF="book-ora065.html#fig-zinc">7.3</A>. 
A simple box represents a
file written
in the language indicated in the box. A double box represents the
interpretation of a
language by a program written in another language. A triple box indicates
that a source
language is compiled to a machine language by using a compiler written
in a third
language. Figure <A HREF="book-ora065.html#fig-t">7.4</A> gives the legend of each box.<BR>
<BR>
<BLOCKQUOTE><DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV>
<DIV ALIGN=center>
<IMG SRC="book-ora027.gif">
<BR>
<BR>
<DIV ALIGN=center>Figure 7.4: Graphical notation for interpreters and compilers.</DIV><BR>

<A NAME="fig-t"></A>
</DIV>
<DIV ALIGN=center><HR WIDTH="80%" SIZE=2></DIV></BLOCKQUOTE>The legend of figure <A HREF="book-ora065.html#fig-zinc">7.3</A> is as follows:<BR>
<BR>
<UL>
<LI>
 BC&nbsp;: Zinc bytecode;

<LI> C&nbsp;: C code;

<LI> .o&nbsp;: object code

<LI> �&nbsp;: micro-processor;

<LI> OC (v1 or v2)&nbsp;: Objective CAML code.
</UL>

<H3> Note </H3> <HR>

The majority of the Objective CAML compiler is written in Objective CAML. The
second part of
figure <A HREF="book-ora065.html#fig-zinc">7.3</A> shows how to pass from version v1 of a compiler to
version v2.


<HR>

<BR>
<BR>
<HR>
<A HREF="book-ora064.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<A HREF="book-ora066.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
